package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.GameCommentDao;
import com.zwxbc.funplay.entity.GameComment;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GameCommentDaoImpl implements GameCommentDao {
	private static final String INSERT_COMMENT = "INSERT INTO game_comment (game_id, player_id, comment, rating, comment_date) VALUES (?, ?, ?, ?, ?)";
	private static final String SELECT_COMMENT_BY_ID = "SELECT * FROM game_comment WHERE comment_id = ?";
	private static final String SELECT_ALL_COMMENTS = "SELECT * FROM game_comment";
	private static final String UPDATE_COMMENT = "UPDATE game_comment SET game_id = ?, player_id = ?, comment = ?, rating = ?, comment_date = ? WHERE comment_id = ?";
	private static final String DELETE_COMMENT = "DELETE FROM game_comment WHERE comment_id = ?";
	private static final String SELECT_COMMENT_BY_GAME_ID = "SELECT * FROM game_comment WHERE game_id = ?";

	@Override
	public int addGameComment(GameComment comment) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_COMMENT)) {
			preparedStatement.setLong(1, comment.getGameId());
			preparedStatement.setLong(2, comment.getPlayerId());
			preparedStatement.setString(3, comment.getComment());
			preparedStatement.setBigDecimal(4, comment.getRating());
			preparedStatement.setDate(5, comment.getCommentDate() != null ? new java.sql.Date(comment.getCommentDate().getTime()) : null);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public List<GameComment> getGameCommentListByGameId(Long gameId) {
		List<GameComment> gameComments = new ArrayList<>();

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_COMMENT_BY_GAME_ID)) {
			// 设置查询参数
			preparedStatement.setLong(1, gameId);

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					gameComments.add(createCommentFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}

		return gameComments;
	}

	@Override
	public GameComment getGameCommentById(Long commentId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_COMMENT_BY_ID)) {
			preparedStatement.setLong(1, commentId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createCommentFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<GameComment> getAllGameComments() {
		List<GameComment> comments = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_COMMENTS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				comments.add(createCommentFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return comments;
	}

	@Override
	public int updateGameComment(GameComment comment) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_COMMENT)) {
			preparedStatement.setLong(1, comment.getGameId());
			preparedStatement.setLong(2, comment.getPlayerId());
			preparedStatement.setString(3, comment.getComment());
			preparedStatement.setBigDecimal(4, comment.getRating());
			preparedStatement.setDate(5, comment.getCommentDate() != null ? new java.sql.Date(comment.getCommentDate().getTime()) : null);
			preparedStatement.setLong(6, comment.getCommentId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteGameComment(Long commentId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_COMMENT)) {
			preparedStatement.setLong(1, commentId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 GameComment 对象
	private GameComment createCommentFromResultSet(ResultSet resultSet) throws SQLException {
		GameComment comment = new GameComment();
		comment.setCommentId(resultSet.getLong("comment_id"));
		comment.setGameId(resultSet.getLong("game_id"));
		comment.setPlayerId(resultSet.getLong("player_id"));
		comment.setComment(resultSet.getString("comment"));
		comment.setRating(resultSet.getBigDecimal("rating"));
		comment.setCommentDate(resultSet.getDate("comment_date"));
		return comment;
	}
}
